package com.sparrow.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.File;
import java.lang.reflect.Type;
import java.util.*;

/**
 * jackson json工具类
 */
public class JsonUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final TypeFactory typeFactory = objectMapper.getTypeFactory();

    public static ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    public static String toJSON(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toFormatJSON(Object object) {
        String json = toJSON(object);
        return formatJSON(json);
    }

    public static <T> T parse(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T parse(String json, TypeReference<T> type) {
        try {
            return objectMapper.readValue(json, type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T parse(File file, TypeReference<T> type) {
        try {
            return objectMapper.readValue(file, type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String formatJSON(String jsonString) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
            Object jsonObject = objectMapper.readValue(jsonString, Object.class);
            String formattedJson = objectMapper.writeValueAsString(jsonObject);
            return formattedJson;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T parseArray(String str, Class<T> clazz) {
        try {
            return objectMapper.readValue(str, clazz);
        } catch (Exception e) {
            throw new RuntimeException("JSON 解析失败: " + str, e);
        }
    }

    public static <T> List<T> parseList(String json, Class<? extends T> clazz) {
        try {
            return objectMapper.readValue(json, typeFactory.constructCollectionType(ArrayList.class, clazz));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> parseSet(String json, Class<? extends T> clazz) {
        try {
            return objectMapper.readValue(json, typeFactory.constructCollectionType(HashSet.class, clazz));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static <K, V> Map<K,V> parseMap(String json, Class<? extends K> kClass, Class<? extends V> vClass) {
        try {
            return objectMapper.readValue(json, typeFactory.constructMapType(HashMap.class, kClass, vClass));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String json = "[\"apple\", \"banana\", \"cherry\"]";
        List<String> list = JsonUtil.parseList(json, String.class);
        System.out.println("Parsed List: " + list);

        String json2 = "{\"name\": \"Alice\", \"age\": 25}";
        Map<String, Object> map = JsonUtil.parseMap(json2, String.class, Object.class);
        System.out.println("Parsed Map: " + map);
    }

}
